<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		ScriptPlanet.eu															/
/	Site:			http://www.scriptplanet.eu												/
/	Email:			support@scriptplanet.eu													/
/	Support:		http://www.scriptplanet.eu/support										/
/	Copyright:		2011 (c) ScriptPlanet.eu												/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0002																	/
/																							/
/========================================== JooSam ========================================*/
// Joomla Security
defined('_JEXEC') or die;
JHTML::_('behavior.mootools');
jimport('joomla.application.component.helper');
if(JComponentHelper::isEnabled('com_joosam', true)):
	$joosam_plugin = JPluginHelper::isEnabled('system', 'joosam');
	if($joosam_plugin):

		// Get all Config/Params

		$global_modPosition =		$params->get('mod_direction');				// 0 = horizontal, 1 = Vertical
		$global_imageLoad =			$params->get('mod_image');					// 0 = no, 1 = yes
		$global_imageWidth =		$params->get('mod_image_width' ,175);		// Image Width
		$global_imageHeight =		$params->get('mod_image_height' ,175);		// Image Height
		$global_timeMarge =			$params->get('time_marge');					// Between -120 and 120
		$global_offlineTXT =		$params->get('mod_offline');				// Text when player is offline
		$color_artistLabel =		$params->get('col_artist_label');			// Color Artist Label
		$color_artistData =			$params->get('col_artist_value');			// Color Artist Data
		$color_titleLabel =			$params->get('col_title_label');			// Color Title Label
		$color_titleData =			$params->get('col_title_value');			// Color Title Data
		$color_timer =				$params->get('col_timer');					// Color Timer
		$thanks_activate =			$params->get('thanks_active');				// Thanks msg activate: 0 = Disable 1 = Enable
		$thanks_typeLoad =			$params->get('thanks_type');				// topload = Load div on top page / marqueload = Replace a marquee text / textbaloon = Create text baloon by module
		$thanks_htmlString =		$params->get('thanks_string');				// String - html when thanks to load
		$thanks_marqueeID =			$params->get('thanks_marq_id');				// Marquee htmltag ID (replace this innerhtml)
		$thanks_marqueeDefault =	$params->get('thanks_marq_default');		// Marquee Default text. 
		$thanks_baloonPosition =	$params->get('thanks_baloon_position');		// Position of the text baloon
		$thanks_baloonColor =		$params->get('thanks_baloon_color');		// Color of the text baloon
		$thanks_baloonTrans =		$params->get('thanks_baloon_transparant');	// Text baloon Transparant
		$thanks_baloonYas =			$params->get('thanks_baloon_marge_yas');	// Baloon extra Y as marge
		$thanks_baloonXas =			$params->get('thanks_baloon_marge_xas');	// Baloon extra X as marge
		$thanks_baloontxtColor =	$params->get('thanks_baloon_txtcolor');		// Baloon Text color
		$thanks_htmlString = str_replace('"', '\"', $thanks_htmlString);

		// css work (only by Textbaloon needed
		if($thanks_typeLoad == 'textbaloon'):
			// Image
			if($thanks_baloonTrans == 1 and $thanks_baloonColor != 'custom'){
				$box_image = JURI::root().'modules/mod_joosam_live/baloons/trans/'.$thanks_baloonColor.'/'.$thanks_baloonPosition.'.png';
			} else{
				$box_image = JURI::root().'modules/mod_joosam_live/baloons/'.$thanks_baloonColor.'/'.$thanks_baloonPosition.'.png';
			}
			
			// Other css
			if($thanks_baloonPosition == 'top_left'){
				$box_padding = '10px 10px 40px 10px';
				$box_x = -250 + $thanks_baloonXas;
				$box_y = -30 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'top_center'){
				$box_padding = '10px 10px 40px 10px';
				$box_x = -50 + $thanks_baloonXas;
				$box_y = -60 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'top_right'){
				$box_padding = '10px 10px 40px 10px';
				$box_x = 170 + $thanks_baloonXas;
				$box_y = -30 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'mid_right'){
				$box_padding = '10px 10px 10px 80px';
				$box_x = 200 + $thanks_baloonXas;
				$box_y = 20 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'mid_left'){
				$box_padding = '10px 80px 10px 10px';
				$box_x = -300 + $thanks_baloonXas;
				$box_y = 20 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'bot_left'){
				$box_padding = '40px 10px 10px 10px';
				$box_x = -250 + $thanks_baloonXas;
				$box_y = 250 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'bot_center'){
				$box_padding = '40px 10px 10px 10px';
				$box_x = -50 + $thanks_baloonXas;
				$box_y = 250 + $thanks_baloonYas;
			}

			else if($thanks_baloonPosition == 'bot_right'){
				$box_padding = '40px 10px 10px 10px';
				$box_x = 170 + $thanks_baloonXas;
				$box_y = 250 + $thanks_baloonYas;
			}

			else{
				$box_padding = '10px 10px 10px 10px';
				$box_x = 170 + $thanks_baloonXas;
				$box_y = -30 + $thanks_baloonYas;
			}
			?>

			<style>
				#sidebar_right,
				#sidebar_left,
				.module{
					overflow:visible;
				}
				
				#jsm_thanksbaloon{
					background:none;
					background-image: url(<?php echo $box_image; ?>);
					background-size:100% 100%;
					background-repeat:no-repeat;
					padding:<?php echo $box_padding; ?>;
					z-index: 200;
					position:absolute;
					top:<?php echo $box_y; ?>px;
					left: <?php echo $box_x; ?>px;
					color:<?php echo $thanks_baloontxtColor ?>;
					visibility:visible;
					width:300px;
				}
			</style>
			
			<?php
		endif; ?>
		<div id="jsm_modLive">
			<div id="jsm_liveOffline" style="display:none;"><?php echo $global_offlineTXT; ?></div>
			<div id="jsm_liveOnline" style="display:none;"><?php
				if($global_imageLoad): ?>
					<div id="jsm_liveImage" <?php if($global_modPosition == 0){echo 'style="float:left;"';} ?> ></div><?php
				endif; ?>
				<table id="jsm_liveData" style="width:95%;">
					<tr>
						<td id="jsm_liveArtistLabel" songid="0" style="color:<?php echo $color_artistLabel; ?>;"><?php echo JText::_('MOD_JOOSAM_LIVE_ARTIST'); ?>:</td>
						<td id="jsm_liveArtistData" style="color:<?php echo $color_artistData; ?>;"></td>
					</tr>
					<tr>
						<td id="jsm_liveTitleLabel" style="color:<?php echo $color_titleLabel; ?>;"><?php echo JText::_('MOD_JOOSAM_LIVE_TITLE'); ?>:</td>
						<td id="jsm_liveTitleData" style="color:<?php echo $color_titleData; ?>;"></td>
					</tr>
					<tr>
						<td id="jsm_liveTimer" colspan="2" style="color:<?php echo $color_timer; ?>;" ></td>
					</tr>
				</table>
			</div>
		</div>
		<script>
			
			function jsm_live(){
				var songid = joosam.data_live_songid;
				var artist = joosam.data_live_artist;
				var title = joosam.data_live_title;
				var timer = parseFloat(<?php echo $global_timeMarge; ?>)+parseFloat(joosam.data_live_timer);
				var loadImage = <?php echo $global_imageLoad; ?>;
				var image = joosam.data_live_image;
				var request = <?php echo $thanks_activate; ?>;
				var requestId = joosam.data_live_req_id;
				var requestName = joosam.data_live_req_name;
				var requestMsg = joosam.data_live_req_msg;
				var requestType = "<?php echo $thanks_typeLoad; ?>";
				var requestHtml = "<?php echo $thanks_htmlString; ?>";
				var docSid = document.getElementById("jsm_liveArtistLabel").getAttribute('songid');
				var timecount = '00:00';
				var requestMarquee = "<?php echo $thanks_marqueeID; ?>";
				var requestMarqueeDefault = "<?php echo $thanks_marqueeDefault; ?>";

				// Player offline
				if(songid == 'jsm_false'){
					document.getElementById("jsm_liveOffline").setAttribute('style', 'display:block;');
					document.getElementById("jsm_liveArtistLabel").setAttribute('songid', '0');
					document.getElementById("jsm_liveOnline").setAttribute('style', 'display:none;');
				}

				// Player online
				else{
					document.getElementById("jsm_liveOffline").setAttribute('style', 'display:none;');
					document.getElementById("jsm_liveOnline").setAttribute('style', 'display:block;');
					
					// Song Image
					if(loadImage == 1){
						// Image Alowed - Check running songID for load new
						if(songid != docSid){
							// Load New image
							var imgwidth = '<?php echo $global_imageWidth; ?>px';
							var imgheight = '<?php echo $global_imageHeight; ?>px';
							var imgload = <?php echo $global_imageLoad; ?>;
							if(image != 'jsm_false'){
								image = image.replace("{load_width}", imgwidth);
								image = image.replace("{load_height}", imgheight);
								document.getElementById("jsm_liveImage").innerHTML = image;
							} else{
								document.getElementById("jsm_liveImage").innerHTML = '';
							}
						}
					}

					// Song Title
					if(title != 'jsm_false'){
						document.getElementById("jsm_liveTitleData").innerHTML = title;
					} else{
						document.getElementById("jsm_liveTitleData").innerHTML = '';
					}

					// Song Artist
					if(artist != 'jsm_false'){
						document.getElementById("jsm_liveArtistData").innerHTML =artist;
					} else{
						document.getElementById("jsm_liveArtistData").innerHTML = '';
					}

					// Song Timer
					var timerHours = Math.floor(timer / (60 * 60));
					var timerMinutes = Math.floor((timer % (60 * 60)) / 60);
					var timerSeconds = Math.ceil((timer % (60 * 60)) % 60);
					if(timerHours > 0){ timerHours = timerHours+':'; } else{ timerHours = ''; }
					if(timerMinutes < 10){ timerMinutes = '0'+timerMinutes; }
					else if(timerMinutes >= 10){ timerMinutes = timerMinutes; }
					else{ timerMinutes = ''; }
					if(timerSeconds < 10){ timerSeconds = '0'+timerSeconds; }
					else if(timerSeconds >= 10){ timerSeconds = timerSeconds; }
					else{ timerSeconds = ''; }
					if(timer > 0){
						timecount = timerHours+timerMinutes+':'+timerSeconds;
					}
					document.getElementById("jsm_liveTimer").innerHTML = timecount;

					// Set SongID
					document.getElementById("jsm_liveArtistLabel").setAttribute('songid', songid);
					
					if(requestName == 'jsm_false'){
						requestName = 'Unknown';
					}
					if(requestMsg == 'jsm_false'){
						requestMsg = 'Unknown';
					}
					// Set Thanks Message
					if((request == 1) && (songid != docSid)){

						// Build Output
						if(requestId > 0){
							requestHtml = requestHtml.replace(/{load_name}/g, requestName);
							requestHtml = requestHtml.replace(/{load_msg}/g, requestMsg);
						}

						// For Marquee
						if(requestType == 'marqueload'){
							if(document.getElementById(requestMarquee)){
								if(requestId > 0){
									document.getElementById(requestMarquee).innerHTML = requestHtml;
								} else{
									document.getElementById(requestMarquee).innerHTML = requestMarqueeDefault;
								}
							} else{
								alert("Marquee html id ["+requestMarquee+"] does not exist. It is not possible to set message now.");
							}
						}

						// When Div on top
						else if(requestType == 'topload'){
							if(requestId > 0){
								var build = document.createElement('div');
								build.id = 'jsm_requestdiv';
								if(document.body.firstChild){
									document.body.insertBefore(build, document.body.firstChild);
								} else{
									document.body.appendChild(build);
								}
								var winWidth = window.innerWidth;
								var buildPosX = winWidth / 2 - 150;
								var buildLeft = 'left:'+buildPosX+'px;';
								var docTarget = document.getElementById('jsm_requestdiv');
								var docCss = 'text-align:center; width:300px; padding:0; z-index:10000; '+buildLeft;
								if(docTarget){
									docTarget.setAttribute('style', docCss);
									docTarget.innerHTML = requestHtml;
								}
							} else{
								var docTarget = document.getElementById('jsm_requestdiv');
								if(docTarget){
									document.body.removeChild(docTarget);
								}
							}
						}

						// When TextBaloon
						else if(requestType == 'textbaloon'){
							var livediv = document.getElementById('jsm_modLive');
							if(requestId > 0){								
								var build = document.createElement('div');
								build.id = 'jsm_thanksbaloon';
								build.innerHTML = requestHtml;
								livediv.appendChild(build);
							}
							else{
								var baloon = document.getElementById('jsm_thanksbaloon');
								if(baloon){
									livediv.removeChild(baloon);
								}
							}
						}

					}
				}
				setTimeout('jsm_live()', 1000);
			}
			setTimeout('jsm_live()', 1000);
		</script><?php
	else: ?>
		<div id="jsm_modLive">
			<div style="background:white; color:red; font-weight:bold; border-radius:10px; margin:1px; text-align:center; padding:0;">
				<?php echo JText::_('MOD_JOOSAM_LIVE_NOPLUGIN'); ?>
			</div>
		</div><?php
	endif;
else: ?>
	<div id="jsm_modLive">
		<div style="background:white; color:red; font-weight:bold; border-radius:10px; margin:1px; text-align:center; padding:0;">
			<?php echo JText::_('MOD_JOOSAM_LIVE_NOCOMPONENT'); ?>
		</div>
	</div><?php
endif;
?>